package rs.networx.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;

//-----------------------------------------------------------------------------------------------------------------------------------------------------
public class LogoutServlet extends HttpServlet {

	private static final long serialVersionUID = 8377248947023511875L;

	@EJB
	private PersonDao personDao;

	private String username;
	private String password;

	private Person loggedUser;
	private Person personToLoguot;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("*** Loaded LogoutServlet ***");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		try {
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		System.out.println(username + "  " + password);

		// ako imamo korisnika  na sesiji za logout...
		if(loggedUser != null){
			if(loggedUser.getLoggedIn() == true){
				// i ako je ulogovan...
				personToLoguot = personDao.logout(username, password);
				if(loggedUser.getEmail().equals(personToLoguot.getEmail()) && loggedUser.getPassword().equals(personToLoguot.getPassword())){
					// i ako je objekat sa sesije isti onaj person kojega treba da odlogujemo...
					// tada odlogujemo person i idemo na welcome page...
					loggedUser.setLoggedIn(false);

					response.setHeader("Cache-Control", "no-store");
					session.invalidate();

					request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
					return;
				}else {
					// ako objekat sa sesije nije isti kao person kojega treba da odlogujemo...
					request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
					return;
				}
			}if(loggedUser.getLoggedIn() == false){
				// ako nije ulogovan...
				request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
				return;
			}
		}else if(loggedUser == null){
			// nema koga da odlogujemo, pa idemo na pocetnu stranu...
			request.getRequestDispatcher("WelcomePage.jsp").forward(request, response);
			return;
		}
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	

}
